#include "json/cJSON.h"
#include "log/log.h"
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    //1.生成json字符串  {"name": "tom","age": 18}
    char *n = "tom";
    int a = 18;
    cJSON *root_obj = cJSON_CreateObject();
    cJSON_AddStringToObject(root_obj, "name", n);
    cJSON_AddNumberToObject(root_obj, "age", a);
    char *json_str = cJSON_PrintUnformatted(root_obj);
    log_debug("生成的json字符串为:%s", json_str);

    //2.解析json字符串
    cJSON *root_obj2 = cJSON_Parse(json_str);
    char *name = cJSON_GetObjectItem(root_obj2, "name")->valuestring;
    int age = cJSON_GetObjectItem(root_obj2, "age")->valueint;

    log_debug("解析后的json字符串为:name:%s,age:%d", name, age);

    //3.释放
    cJSON_Delete(root_obj);
    cJSON_Delete(root_obj2);
    free(json_str);

    return 0;
}